package com.yycx.module.weixin.provider.service.impl;

import com.alibaba.fastjson.JSON;
import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.mybatis.base.service.impl.BaseServiceImpl;
import com.yycx.common.mybatis.model.ResultBody;
import com.yycx.common.mybatis.query.CriteriaQuery;
import com.yycx.common.mybatis.query.CriteriaSave;
import com.yycx.module.weixin.client.entity.SysTemplateMessageLog;
import com.yycx.module.weixin.provider.mapper.SysTemplateMsgLogMapper;
import com.yycx.module.weixin.provider.service.SysTemplateMsgLogService;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 接口实现类
 *
 * @author flyme
 * @date 2022-03-01
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class SysTemplateMsgLogServiceImpl extends BaseServiceImpl<SysTemplateMsgLogMapper, SysTemplateMessageLog> implements SysTemplateMsgLogService {

    @Override
    public ResultBody beforeAdd(CriteriaSave cs, SysTemplateMessageLog templatemsglog, EntityMap extra) {
        return ResultBody.ok();
    }


    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public ResultBody beforePageList(CriteriaQuery<SysTemplateMessageLog> cq, SysTemplateMessageLog templatemsglog, EntityMap requestMap) {
        cq.orderByDesc("templatemsglog.createTime");
        return ResultBody.ok();
    }

    @Override
    public SysTemplateMessageLog initTemplateMsgLog(WxMpTemplateMessage templateMessage, Long weChatId, Integer sendState) {
        SysTemplateMessageLog templatemsgLog = new SysTemplateMessageLog();
        templatemsgLog.setTemplateId(templateMessage.getTemplateId());
        templatemsgLog.setOpenId(templateMessage.getToUser());
        templatemsgLog.setUrl(templateMessage.getUrl());
        templatemsgLog.setWeChatId(weChatId);
        List<WxMpTemplateData> data = templateMessage.getData();
        String messageInfo = JSON.toJSONString(data);
        templatemsgLog.setMessageInfo(messageInfo);
        templatemsgLog.setSendState(sendState);
        return templatemsgLog;
    }
}
